package com.ledim.widget.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.letv.android.young.client.R;

public class LeDimListViewHeader extends FrameLayout {

    private FrameLayout mContainer;
    private ImageView mNormal;
    private ImageView mReday;
    private ImageView mProgressBar;
    private TextView mHintTextView;
    private int mState = STATE_NORMAL;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;

    private AnimationDrawable mAnimationDrawable = null;

    public LeDimListViewHeader(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public LeDimListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public LeDimListViewHeader(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        // 初始情况，设置下拉刷新view高度为0
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, 0);
        mContainer =
                (FrameLayout) LayoutInflater.from(context).inflate(R.layout.xlistview_header, null);
        addView(mContainer, lp);

        mNormal = (ImageView) findViewById(R.id.xlistview_header_normal);
        mReday = (ImageView) findViewById(R.id.xlistview_header_reday);
        mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
        mProgressBar = (ImageView) findViewById(R.id.xlistview_header_progressbar);
        mProgressBar.setImageResource(R.anim.refresh_animation);
        mAnimationDrawable = (AnimationDrawable) mProgressBar.getDrawable();
        mAnimationDrawable.setOneShot(false);
        mAnimationDrawable.start();

    }

    public void setState(int state) {
        if (state == mState) {
            return;
        }

        if (state == STATE_REFRESHING) {    // 显示进度
            //			mArrowImageView.clearAnimation();
            mNormal.setVisibility(View.GONE);
            mReday.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.VISIBLE);
        } else {    // 显示箭头图片
            mNormal.setVisibility(View.VISIBLE);
            mReday.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.INVISIBLE);
        }

        switch (state) {
            case STATE_NORMAL:
                if (mState == STATE_READY) {
                    mNormal.setVisibility(View.VISIBLE);
                    mReday.setVisibility(View.GONE);
                }
                if (mState == STATE_REFRESHING) {
                    //				mArrowImageView.clearAnimation();
                }
                mHintTextView.setText(R.string.xlistview_header_hint_normal);
                break;
            case STATE_READY:
                if (mState != STATE_READY) {
                    mNormal.setVisibility(View.GONE);
                    mReday.setVisibility(View.VISIBLE);
                    mHintTextView.setText(R.string.xlistview_header_hint_ready);
                }
                break;
            case STATE_REFRESHING:
                mNormal.setVisibility(View.GONE);
                mReday.setVisibility(View.GONE);
                mHintTextView.setText(R.string.xlistview_header_hint_loading);
                break;
            default:
        }
        mState = state;
    }

    public void setVisiableHeight(int height) {
        if (height < 0) {
            height = 0;
        }
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContainer.getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
    }

    public int getVisiableHeight() {
        return mContainer.getLayoutParams().height;
    }

}
